﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using Microsoft.Phone.Tasks;
using Microsoft.Phone;

namespace ProtoImaginer.Project
{
    public partial class SelectPicture : PhoneApplicationPage
    {
        ApplicationBarIconButton btnCamera;
        ApplicationBarIconButton btnCrop;
        ApplicationBarIconButton btnHelp;

        //Variable for the help popup
        Popup help = new Popup();
        CameraCaptureTask ctask;
        public SelectPicture()
        {
            InitializeComponent();
            //Creates an application bar and then sets visibility and menu properties.
            ApplicationBar = new ApplicationBar();
            ApplicationBar.IsVisible = true;

            //This code creates the application bar icon buttons.
            btnCamera = new ApplicationBarIconButton(new Uri("/Icons/appbar.feature.camera.rest.png", UriKind.Relative));
            btnCrop = new ApplicationBarIconButton(new Uri("/Icons/appbar.edit.rest.png", UriKind.Relative));
            btnHelp = new ApplicationBarIconButton(new Uri("/Icons/appbar.questionmark.rest.png", UriKind.Relative));

            //Labels for the application bar buttons.
            btnCamera.Text = "Camera";
            btnCrop.Text = "Crop";
            btnHelp.Text = "Help";

            //This code will create event handlers for buttons.
            btnCamera.Click += new EventHandler(btnCamera_Click);
            btnCrop.Click += new EventHandler(btnCrop_Click);
            btnHelp.Click += new EventHandler(btnHelp_Click);

            //This code adds buttons to application bar.
            ApplicationBar.Buttons.Add(btnCamera);
            ApplicationBar.Buttons.Add(btnCrop);
            ApplicationBar.Buttons.Add(btnHelp);

            //Disable crop button until photo is taken.
            btnCrop.IsEnabled = false;
            ctask = new CameraCaptureTask();
            textStatus.Text = "Tap the camera button to take a picture.";
        }
        void btnCamera_Click(object sender, EventArgs e)
        {
            textStatus.Text = "";

            //Show the camera.
            ctask.Show();
        }

        void ctask_Completed(object sender, PhotoResult e)
        {

            if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
            {
                App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
                photo.Source = App.CapturedImage;
                //Once writeable bitmap has been rendered, the crop button is enabled.
                btnCrop.IsEnabled = true;
                textStatus.Text = "Tap the crop button to proceed";
            }
            else
            {
                textStatus.Text = "You decided not to take a picture.";
            }
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            //Stack panel with a black background.
            StackPanel panelHelp = new StackPanel();
            panelHelp.Background = new SolidColorBrush(Colors.Black);
            panelHelp.Width = 300;
            panelHelp.Height = 400;

            //Create a white border.
            Border border = new Border();
            border.BorderBrush = new SolidColorBrush(Colors.White);
            border.BorderThickness = new Thickness(7.0);

            //Create a close button to exit popup.
            Button close = new Button();
            close.Content = "Close";
            close.Margin = new Thickness(5.0);
            close.Click += new RoutedEventHandler(close_Click);

            //Create helper text
            TextBlock textblockHelp = new TextBlock();
            textblockHelp.FontSize = 24;
            textblockHelp.Foreground = new SolidColorBrush(Colors.White);
            textblockHelp.TextWrapping = TextWrapping.Wrap;
            textblockHelp.Text = "Tap the camera button image on the application bar to take a photo." + " Once the photo is taken and returned to this page, tap the crop button on the application bar to crop the image.";
            textblockHelp.Margin = new Thickness(5.0);

            //Add controls to stack panel
            panelHelp.Children.Add(textblockHelp);
            panelHelp.Children.Add(close);
            border.Child = panelHelp;

            // Set the Child property of Popup to the border 
            // that contains a stackpanel, textblock and button.
            help.Child = border;

            // Set where the popup will show up on the screen.   
            help.VerticalOffset = 200;
            help.HorizontalOffset = 85;

            // Open the popup.
            help.IsOpen = true;
        }
        private void close_Click(object sender, RoutedEventArgs e)
        {
            help.IsOpen = false;
        }
        private void btnCrop_Click(object sender, EventArgs e)
        {
            //Error text for if user does not take a photo before choosing the crop button.
            if (App.CapturedImage == null)
            {
                textStatus.Text = "You must take a picture first.";
            }
            else
            {
                //If photo has been taken, crop button navigates to Crop.xaml page.
                NavigationService.Navigate(new Uri("/Crop.xaml", UriKind.Relative));
            }
        }
    }
}